﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data;
using CardLogistics.Model.Purchase;

namespace CardLogistics.DAL.Purchase
{
    /// <summary>
    /// 采购合同明细 数据访问类:PURCHASE_CONTRACT_DETAIL
    /// </summary>
    public class Purchase_Contract_detailData
    {
        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "v_Purchase_Contract_detail", StrFields, "ID", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }

        /// <summary>
        /// 通过合同ID得到合同明细信息
        /// </summary>
        /// <param name="conId">合同ID</param>
        /// <returns></returns>
        public DataTable GetInfoByConId(string conId)
        {
            return GetList(" ConId = '" + conId + "' ", "*");
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Purchase_Contract_detail GetModel(string rId)
        {
            Purchase_Contract_detail model = new Purchase_Contract_detail();
            DataRow row = GetInfoById(rId).Rows[0];
            if (row != null)
            {
                if (row["ID"] != null)
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["CONID"] != null && row["CONID"].ToString() != "")
                {
                    model.CONID = row["CONID"].ToString();
                }
                if (row["CARDTYPEID"] != null && row["CARDTYPEID"].ToString() != "")
                {
                    model.CARDTYPEID = row["CARDTYPEID"].ToString();
                }
                if (row["CHIPID"] != null && row["CHIPID"].ToString() != "")
                {
                    model.CHIPID = row["CHIPID"].ToString();
                }
                if (row["COSVERSION"] != null && row["COSVERSION"].ToString() != "")
                {
                    model.COSVERSION = row["COSVERSION"].ToString();
                }
                if (row["PRICE"] != null && row["PRICE"].ToString() != "")
                {
                    model.PRICE = decimal.Parse(row["PRICE"].ToString());
                }
                if (row["CARDAMOUNT"] != null && row["CARDAMOUNT"].ToString() != "")
                {
                    model.CARDAMOUNT = decimal.Parse(row["CARDAMOUNT"].ToString());
                }
                if (row["SPAREPARTS"] != null && row["SPAREPARTS"].ToString() != "")
                {
                    model.SPAREPARTS = decimal.Parse(row["SPAREPARTS"].ToString());
                }
                if (row["PREMIUMS"] != null && row["PREMIUMS"].ToString() != "")
                {
                    model.PREMIUMS = decimal.Parse(row["PREMIUMS"].ToString());
                }

            }
            return model;
        }

        #endregion

        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(Purchase_Contract_detail model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("v_varOut", OracleType.VarChar,50),
					new OracleParameter("V_CONID", OracleType.VarChar,50),
					new OracleParameter("V_CARDTYPEID", OracleType.VarChar,50),
					new OracleParameter("V_CHIPID", OracleType.VarChar,50),
					new OracleParameter("V_COSVERSION", OracleType.VarChar,50),
					new OracleParameter("V_PRICE", OracleType.Number,4),
					new OracleParameter("V_CARDAMOUNT", OracleType.Number,4),
					new OracleParameter("V_SPAREPARTS", OracleType.Number,4),
					new OracleParameter("V_PREMIUMS", OracleType.Number,4)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.CONID;
                parameters[2].Value = model.CARDTYPEID;
                parameters[3].Value = model.CHIPID;
                parameters[4].Value = model.COSVERSION;
                parameters[5].Value = model.PRICE;
                parameters[6].Value = model.CARDAMOUNT;
                parameters[7].Value = model.SPAREPARTS;
                parameters[8].Value = model.PREMIUMS;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Purchase_Contract_detail_ADD", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(Purchase_Contract_detail model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("V_ID", OracleType.VarChar,50),
					new OracleParameter("V_CONID", OracleType.VarChar,50),
					new OracleParameter("V_CARDTYPEID", OracleType.VarChar,50),
					new OracleParameter("V_CHIPID", OracleType.VarChar,50),
					new OracleParameter("V_COSVERSION", OracleType.VarChar,50),
					new OracleParameter("V_PRICE", OracleType.Number,4),
					new OracleParameter("V_CARDAMOUNT", OracleType.Number,4),
					new OracleParameter("V_SPAREPARTS", OracleType.Number,4),
					new OracleParameter("V_PREMIUMS", OracleType.Number,4),
                    new OracleParameter("v_varOut", OracleType.VarChar,50)};
                parameters[0].Value = model.ID;
                parameters[1].Value = model.CONID;
                parameters[2].Value = model.CARDTYPEID;
                parameters[3].Value = model.CHIPID;
                parameters[4].Value = model.COSVERSION;
                parameters[5].Value = model.PRICE;
                parameters[6].Value = model.CARDAMOUNT;
                parameters[7].Value = model.SPAREPARTS;
                parameters[8].Value = model.PREMIUMS;
                parameters[9].Direction = ParameterDirection.Output;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Purchase_Contract_detail_Update", parameters, parameters[9]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("VarOut",  OracleType.VarChar,100),
					    new OracleParameter("v_id",  OracleType.VarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Purchase_Contract_detail_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
